/*
 * (C) Copyright 2014-2016, by Dimitrios Michail
 *
 * JHeaps Library
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jheaps.monotone

import junit.framework.TestCase.assertEquals
import org.jheaps.AddressableHeap
import org.junit.Assert
import org.junit.Test
import java.io.*
import java.util.*

class DoubleRadixAddressableHeapTest {
    @Test
    fun testBug1() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, 3.667944409236726)
        h.insert(0.0)
        assertEquals(0.0, h.findMin().key, 1e-9)
        h.insert(0.9169861023091815)
        h.deleteMin()
        assertEquals(0.9169861023091815, h.findMin().key, 1e-9)
        h.insert(1.7814708581727154)
        h.deleteMin()
        assertEquals(1.7814708581727154, h.findMin().key, 1e-9)
    }

    @Test
    fun testBug2() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, 100.0)
        h.insert(0.0)
        assertEquals(0.0, h.findMin().key, 1e-9)
        assertEquals(0.0, h.deleteMin().key, 1e-9)
        h.insert(15.0)
        assertEquals(15.0, h.findMin().key, 1e-9)
    }

    @Test
    fun testVerySmall() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(15.0, 50.5)

        h.insert(15.3)
        h.insert(50.4)
        h.insert(20.999999)
        h.insert(50.5)
        h.insert(30.3)
        h.insert(25.2)
        h.insert(17.7777)

        Assert.assertEquals(7, h.size())
        assertEquals(15.3, h.findMin().key, 1e-9)
        Assert.assertEquals(7, h.size())
        assertEquals(15.3, h.deleteMin().key, 1e-9)
        Assert.assertEquals(6, h.size())
        assertEquals(17.7777, h.findMin().key, 1e-9)
        assertEquals(17.7777, h.deleteMin().key, 1e-9)
        assertEquals(20.999999, h.findMin().key, 1e-9)
        assertEquals(20.999999, h.deleteMin().key, 1e-9)
        assertEquals(25.2, h.findMin().key, 1e-9)
        assertEquals(25.2, h.deleteMin().key, 1e-9)
        assertEquals(30.3, h.findMin().key, 1e-9)
        assertEquals(30.3, h.deleteMin().key, 1e-9)
        assertEquals(50.4, h.findMin().key, 1e-9)
        assertEquals(50.4, h.deleteMin().key, 1e-9)
        assertEquals(50.5, h.findMin().key, 1e-9)
        assertEquals(50.5, h.deleteMin().key, 1e-9)
        Assert.assertEquals(h.size(), 0)
        Assert.assertTrue(h.isEmpty)
    }

    @Test
    fun test() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, SIZE.toDouble())

        for (i in 0 until SIZE) {
            h.insert(i.toDouble())
            assertEquals(0.0, h.findMin().key, 1e-9)
            Assert.assertFalse(h.isEmpty)
            Assert.assertEquals(h.size(), (i + 1).toLong())
        }

        for (i in SIZE - 1 downTo 0) {
            assertEquals((SIZE - i - 1).toDouble(), h.findMin().key, 1e-9)
            h.deleteMin()
        }
    }

    @Test
    fun testSortRandomSeed1() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, 1.0)

        val generator = Random(1)

        h.insert(0.0)
        for (i in 1 until SIZE) {
            val d = generator.nextDouble()
            h.insert(d)
        }

        var prev: Double? = null
        var cur: Double
        while (!h.isEmpty) {
            cur = h.deleteMin().key
            if (prev != null) {
                Assert.assertTrue(prev.compareTo(cur) <= 0)
            }
            prev = cur
        }
    }

    @Test
    fun testSortRandomSeed2() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, 1.0)

        val generator = Random(2)

        h.insert(0.0)
        for (i in 1 until SIZE) {
            val d = generator.nextDouble()
            h.insert(d)
        }

        var prev: Double? = null
        var cur: Double
        while (!h.isEmpty) {
            cur = h.deleteMin().key
            if (prev != null) {
                Assert.assertTrue(prev.compareTo(cur) <= 0)
            }
            prev = cur
        }
    }

    @Test
    fun testSameMinMax() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(1.0, 1.0)

        for (i in 0..14) {
            h.insert(1.0)
        }

        Assert.assertEquals(15, h.size())
        for (i in 0..14) {
            assertEquals(1.0, h.deleteMin().key, 1e-9)
        }
        Assert.assertEquals(0, h.size())
    }

    @Test
    fun testMaxDifference() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, Double.MAX_VALUE)

        h.insert(0.0)
        h.insert(Double.MAX_VALUE)

        Assert.assertEquals(2, h.size())
        assertEquals(0.0, h.deleteMin().key, 1e-9)
        assertEquals(Double.MAX_VALUE, h.deleteMin().key, 1e-9)
        Assert.assertEquals(0, h.size())
    }

    @Test
    fun testDelete() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, 15.0)

        val array: Array<AddressableHeap.Handle<Double, Void>> = Array(15){i->h.insert(i.toDouble())}
       /* array = arrayOfNulls<AddressableHeap.Handle<*, *>>(15)
        for (i in 0..14) {
            array[i] = h.insert(i.toDouble())
        }*/

        array[5].delete()
        assertEquals(0.0, h.findMin().key, 1e-9)
        array[7].delete()
        assertEquals(0.0, h.findMin().key, 1e-9)
        array[0].delete()
        assertEquals(1.0, h.findMin().key, 1e-9)
        array[2].delete()
        assertEquals(1.0, h.findMin().key, 1e-9)
        array[1].delete()
        assertEquals(3.0, h.findMin().key, 1e-9)
        array[3].delete()
        assertEquals(4.0, h.findMin().key, 1e-9)
        array[9].delete()
        assertEquals(4.0, h.findMin().key, 1e-9)
        array[4].delete()
        assertEquals(6.0, h.findMin().key, 1e-9)
        array[8].delete()
        assertEquals(6.0, h.findMin().key, 1e-9)
        array[11].delete()
        assertEquals(6.0, h.findMin().key, 1e-9)
        array[6].delete()
        assertEquals(10.0, h.findMin().key, 1e-9)
        array[12].delete()
        assertEquals(10.0, h.findMin().key, 1e-9)
        array[10].delete()
        assertEquals(13.0, h.findMin().key, 1e-9)
        array[13].delete()
        assertEquals(14.0, h.findMin().key, 1e-9)
        array[14].delete()
        Assert.assertTrue(h.isEmpty)
    }

    @Test
    fun testDelete1() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, 10.0)

        val array: Array<AddressableHeap.Handle<Double, Void>> = Array(8){i->h.insert(i.toDouble())}
        /*array = arrayOfNulls<AddressableHeap.Handle<*, *>>(8)
        for (i in 0..7) {
            array[i] = h.insert(i.toDouble())
        }*/

        array[5].delete()
        assertEquals(0.0, h.findMin().key, 1e-9)
        array[7].delete()
        assertEquals(0.0, h.findMin().key, 1e-9)
        array[0].delete()
        assertEquals(1.0, h.findMin().key, 1e-9)
        array[2].delete()
        assertEquals(1.0, h.findMin().key, 1e-9)
        array[1].delete()
        assertEquals(3.0, h.findMin().key, 1e-9)
    }

    @Test
    fun testAddDelete() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, SIZE.toDouble())

        val array: Array<AddressableHeap.Handle<Double, Void>> = Array(SIZE){i->h.insert(i.toDouble())}
       /* array = arrayOfNulls<AddressableHeap.Handle<*, *>>(SIZE)
        for (i in 0 until SIZE) {
            array[i] = h.insert(i.toDouble())
        }*/

        for (i in SIZE - 1 downTo 0) {
            array[i].delete()
            if (i > 0) {
                assertEquals(0.0, h.findMin().key, 1e-9)
            }
        }
        Assert.assertTrue(h.isEmpty)
    }

    @Test
    fun testClear() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, 15.0)

        for (i in 0..14) {
            h.insert(i.toDouble())
        }

        h.clear()
        Assert.assertEquals(0L, h.size())
        Assert.assertTrue(h.isEmpty)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDeleteTwice() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, 15.0)

        val array: Array<AddressableHeap.Handle<Double, Void>> = Array(15){i->h.insert(i.toDouble())}
       /* array = arrayOfNulls<AddressableHeap.Handle<*, *>>(15)
        for (i in 0..14) {
            array[i] = h.insert(i.toDouble())
        }*/

        array[5].delete()
        assertEquals(0.0, h.findMin().key, 1e-9)
        array[7].delete()
        assertEquals(0.0, h.findMin().key, 1e-9)
        array[0].delete()
        assertEquals(1.0, h.findMin().key, 1e-9)
        array[2].delete()
        assertEquals(1.0, h.findMin().key, 1e-9)
        array[1].delete()
        assertEquals(3.0, h.findMin().key, 1e-9)
        array[3].delete()
        assertEquals(4.0, h.findMin().key, 1e-9)
        array[9].delete()
        assertEquals(4.0, h.findMin().key, 1e-9)
        array[4].delete()
        assertEquals(6.0, h.findMin().key, 1e-9)

        // again
        array[2].delete()
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDeleteMinDeleteTwice() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, 100.0)
        val e1 = h.insert(50.0)
        h.insert(100.0)
        h.deleteMin()
        e1.delete()
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDeleteMinDeleteTwice1() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(99.0, 200.0)

        for (i in 100..199) {
            h.insert(i.toDouble())
        }

        h.deleteMin().delete()
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDeleteMinDecreaseKey() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(100.0, 200.0)

        for (i in 100..199) {
            h.insert(i.toDouble())
        }
        h.deleteMin().decreaseKey(0.0)
    }

    @Test
    fun testDecreaseKey() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, 200.0)

        h.insert(0.0) // monotone

        val array: Array<AddressableHeap.Handle<Double, Void>> = Array(15){i->h.insert(i.toDouble()+100.0)}
        /*array = arrayOfNulls<AddressableHeap.Handle<*, *>>(15)

        for (i in 0..14) {
            array[i] = h.insert(i.toDouble() + 100.0)
        }*/

        array[5].decreaseKey(5.0)
        array[1].decreaseKey(50.0)
        array[10].decreaseKey(3.0)
        array[0].decreaseKey(1.0)
        array[5].delete()
        array[2].delete()

        assertEquals(0.0, h.deleteMin().key, 1e-9)
        assertEquals(1.0, h.deleteMin().key, 1e-9)
        assertEquals(3.0, h.deleteMin().key, 1e-9)
        assertEquals(50.0, h.deleteMin().key, 1e-9)
        assertEquals(103.0, h.deleteMin().key, 1e-9)
        assertEquals(104.0, h.deleteMin().key, 1e-9)

        array[14].decreaseKey(107.5)
        array[13].decreaseKey(108.5)

        assertEquals(106.0, h.deleteMin().key, 1e-9)
        assertEquals(107.0, h.deleteMin().key, 1e-9)
        assertEquals(107.5, h.deleteMin().key, 1e-9)
        assertEquals(108.0, h.deleteMin().key, 1e-9)
        assertEquals(108.5, h.deleteMin().key, 1e-9)
        assertEquals(109.0, h.deleteMin().key, 1e-9)
        assertEquals(111.0, h.deleteMin().key, 1e-9)
        assertEquals(112.0, h.deleteMin().key, 1e-9)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIncreaseKey() {
        val h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap(0.0, 200.0)

        val array: Array<AddressableHeap.Handle<Double, Void>> = Array(15){i->h.insert(i.toDouble()+100.0)}
       /* array = arrayOfNulls<AddressableHeap.Handle<*, *>>(15)
        for (i in 0..14) {
            array[i] = h.insert(i.toDouble() + 100.0)
        }*/

        assertEquals(100.0, h.findMin().key)
        array[5].decreaseKey(5.0)
        assertEquals(5.0, h.findMin().key)
        array[1].decreaseKey(102.0)
    }

    @Test
    @Throws(IOException::class, ClassNotFoundException::class)
    fun testSerializable() {
        var h: AddressableHeap<Double, Void> = DoubleRadixAddressableHeap<Void>(0.0, 15.0)

        for (i in 0..14) {
            h.insert(i.toDouble())
        }

        // write
        val baos = ByteArrayOutputStream()
        val oos = ObjectOutputStream(baos)
        oos.writeObject(h)
        oos.close()
        val data = baos.toByteArray()

        // read
        val ois = ObjectInputStream(ByteArrayInputStream(data))
        val o = ois.readObject()
        ois.close()
        h = o as AddressableHeap<Double, Void>

        for (i in 0..14) {
            Assert.assertEquals((15 - i).toLong(), h.size())
            assertEquals(i.toDouble(), h.findMin().key, 1e-9)
            h.deleteMin()
        }
        Assert.assertTrue(h.isEmpty)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIllegalConstruction() {
        DoubleRadixAddressableHeap<Void>(-1.0, 10.0)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIllegalConstruction1() {
        DoubleRadixAddressableHeap<Void>(10.0, 9.0)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIllegalConstruction2() {
        DoubleRadixAddressableHeap<Void>(Double.NEGATIVE_INFINITY, 9.0)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIllegalConstruction3() {
        DoubleRadixAddressableHeap<Void>(0.0, Double.POSITIVE_INFINITY)
    }

    companion object {
        private const val SIZE = 100000
    }
}
